package com.sqfw.project.wpReport.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.sqfw.common.utils.StringUtils;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.framework.web.page.PageDomain;
import com.sqfw.framework.web.page.TableSupport;
import com.sqfw.project.system.domain.SysUser;
import com.sqfw.project.system.mapper.SysUserMapper;
import com.sqfw.project.wp.domain.WpInfo;
import com.sqfw.project.wpReport.domain.SeatWpInfo;
import com.sqfw.project.wpReport.domain.WpInfoEvaluate;
import com.sqfw.project.wpReport.domain.dto.YuyinDTO;
import com.sqfw.project.zj.mapper.ZjRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.project.wpReport.domain.SeatServiceReport;
import com.sqfw.project.wpReport.service.ISeatServiceReportService;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.common.utils.poi.ExcelUtil;
import com.sqfw.framework.web.page.TableDataInfo;

/**
 * 话务员服务满意度评测统计表Controller
 *
 * @author lx
 * @date 2023-08-01
 */
@RestController
@RequestMapping("/wpReport/seatServiceReport")
public class SeatServiceReportController extends BaseController
{
    @Autowired
    private ISeatServiceReportService seatServiceReportService;

    @Autowired
    private SysUserMapper sysUserMapper;

    //业务组组部门id
    private static final Long deptId = 101L;

    /**
     * 查询话务员服务满意度评测统计表列表
     */
    @PreAuthorize("@ss.hasPermi('getSeatServiceReportList')")
    @GetMapping("/getSeatServiceReportList")
    public TableDataInfo getSeatServiceReportList(YuyinDTO yuyinDto)
    {
        List<SeatServiceReport> reports = new ArrayList<>();
        SysUser sysUser = new SysUser();
        sysUser.setCode(yuyinDto.getCode());
        sysUser.setNickName(yuyinDto.getNickName());
        sysUser.setDeptId(deptId);
        List<SysUser> sysUsers = sysUserMapper.selectUserList(sysUser);
        //未查询到对应坐席人员,直接返回
        if (sysUsers.size() == 0) {
            return getDataTable(reports);
        }
        List<String> codes = sysUsers.stream().map(SysUser::getCode).collect(Collectors.toList());
        if ("day".equals(yuyinDto.getDateType())){
            startPage();
            //查询呼出总量
            YuyinDTO yuyinDTO = new YuyinDTO();
            yuyinDTO.setTimeStart(yuyinDto.getBegainTime());
            yuyinDTO.setTimeEnd(yuyinDto.getEndTime());
            yuyinDTO.setType("1");

            yuyinDTO.setAgentIds(codes);
            TableDataInfo yuyinCount = seatServiceReportService.getYuyinCount(yuyinDTO);
            List<YuyinDTO> rows = (List<YuyinDTO>)yuyinCount.getRows();
            TableDataInfo tableDataInfo = seatServiceReportService.getSeatServiceReportList(yuyinDto,rows);
            tableDataInfo.setTotal(yuyinCount.getTotal());
            return tableDataInfo;
        }

        if ("month".equals(yuyinDto.getDateType())){
            startPage();
            //查询呼出总量
            YuyinDTO yuyinDTO = new YuyinDTO();
            yuyinDTO.setTimeStart(yuyinDto.getBegainTime());
            yuyinDTO.setTimeEnd(yuyinDto.getEndTime());
            yuyinDTO.setType("2");

            yuyinDTO.setAgentIds(codes);
            TableDataInfo yuyinCount = seatServiceReportService.getYuyinCount(yuyinDTO);
            List<YuyinDTO> rows = (List<YuyinDTO>)yuyinCount.getRows();
            /*List<YuyinDTO> rows = new ArrayList<>();
            YuyinDTO yuyinDTO1 = new YuyinDTO();
            yuyinDTO1.setType("2");
            yuyinDTO1.setTime("2024-05");
            yuyinDTO1.setAgentId("admin");
            yuyinDTO1.setTotal(20);
            yuyinDTO1.setConnectingCount(13);
            rows.add(yuyinDTO1);*/
            TableDataInfo tableDataInfo = seatServiceReportService.getSeatServiceReportList(yuyinDto,rows);
            tableDataInfo.setTotal(yuyinCount.getTotal());
            return tableDataInfo;
        }
        return null;
    }

    /**
     * 导出话务员服务满意度评测统计表列表
     */
    @Log(title = "话务员服务满意度评测统计表", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('seatServiceReport:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, YuyinDTO yuyinDto)
    {
        SysUser sysUser = new SysUser();
        sysUser.setCode(yuyinDto.getCode());
        sysUser.setNickName(yuyinDto.getNickName());
        sysUser.setDeptId(deptId);
        List<SysUser> sysUsers = sysUserMapper.selectUserList(sysUser);
        //未查询到对应坐席人员,直接返回
        if (sysUsers.size() == 0) {
            return ;
        }
        List<String> codes = sysUsers.stream().map(SysUser::getCode).collect(Collectors.toList());

        if ("day".equals(yuyinDto.getDateType())){
            startPage();
            //查询呼出总量
            YuyinDTO yuyinDTO = new YuyinDTO();
            yuyinDTO.setTimeStart(yuyinDto.getBegainTime());
            yuyinDTO.setTimeEnd(yuyinDto.getEndTime());
            yuyinDTO.setType("1");

            yuyinDTO.setAgentIds(codes);
            TableDataInfo yuyinCount = seatServiceReportService.getYuyinCount(yuyinDTO);
            List<YuyinDTO> rows = (List<YuyinDTO>)yuyinCount.getRows();
            TableDataInfo tableDataInfo = seatServiceReportService.getSeatServiceReportList(yuyinDto,rows);
            tableDataInfo.setTotal(yuyinCount.getTotal());
            List<SeatServiceReport> list = (List<SeatServiceReport>) tableDataInfo.getRows();

            ExcelUtil<SeatServiceReport> util = new ExcelUtil<SeatServiceReport>(SeatServiceReport.class);
            util.exportExcel(response, list, "话务员服务满意度评测统计表数据");
        }

        if ("month".equals(yuyinDto.getDateType())){

            //查询呼出总量
            YuyinDTO yuyinDTO = new YuyinDTO();
            yuyinDTO.setTimeStart(yuyinDto.getBegainTime());
            yuyinDTO.setTimeEnd(yuyinDto.getEndTime());
            yuyinDTO.setType("2");

            yuyinDTO.setAgentIds(codes);
            TableDataInfo yuyinCount = seatServiceReportService.getYuyinCount(yuyinDTO);
            List<YuyinDTO> rows = (List<YuyinDTO>)yuyinCount.getRows();
            TableDataInfo tableDataInfo = seatServiceReportService.getSeatServiceReportList(yuyinDto,rows);
            tableDataInfo.setTotal(yuyinCount.getTotal());
            List<SeatServiceReport> list = (List<SeatServiceReport>) tableDataInfo.getRows();

            ExcelUtil<SeatServiceReport> util = new ExcelUtil<SeatServiceReport>(SeatServiceReport.class);
            util.exportExcel(response, list, "话务员服务满意度评测统计表数据");
        }

    }

    /**
     * 业务统计列表深挖查询工单
     * @param seatServiceReport
     * @return
     */
    @PreAuthorize("@ss.hasPermi('selectSeatWpInfoList')")
    @GetMapping("/selectSeatWpInfoList")
    public TableDataInfo selectSeatWpInfoList(SeatServiceReport seatServiceReport) {
        startPage();
        List<SeatWpInfo> wpInfos = seatServiceReportService.selectWpInfoSeatService(seatServiceReport);
        return getDataTable(wpInfos);

    }

    /**
     * 导出话务员服务满意度评测统计表列表
     */
    @Log(title = "话务员服务满意度评测统计表深挖导出", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('exportSeatWpInfo')")
    @PostMapping("/exportSeatWpInfo")
    public void exportSeatWpInfo(HttpServletResponse response, SeatServiceReport seatServiceReport)
    {
        List<SeatWpInfo> list = seatServiceReportService.selectWpInfoSeatService(seatServiceReport);
        ExcelUtil<SeatWpInfo> util = new ExcelUtil<SeatWpInfo>(SeatWpInfo.class);
        util.exportExcel(response, list, "导出信息");
    }


}
